package cn.jinjimi.app.main.adapter;

import android.animation.Animator;
import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.framework.helper.annotation.EventTraceWhenTrue;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.entity.InfoMenu;
import cn.jinjimi.app.managers.NoticeManager;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.framework.router.RouterHelper;
import cn.jinjimi.app.main.adapter.recycler.MyBaseRecyclerAdapter;

import java.util.List;

/**
 * Created by Rain on 2016/2/26.
 */
public class MeMenuAdapter<E> extends MyBaseRecyclerAdapter<E, MeMenuAdapter.ViewHolder> {
    //展示类型
    private final int DISPLAY_TYPE_TITLE = 0;
    private final int DISPLAY_TYPE_SINGLE = 1;
    private final int DISPLAY_TYPE_DOUBLE = 2;

    private Context mContext;

    public MeMenuAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        Object obj = getItem(position);
        if (obj instanceof InfoMenu) {
            InfoMenu infoMenu = (InfoMenu) obj;
            if (infoMenu != null && infoMenu.moduleCode != null && infoMenu.moduleCode.equals("title"))
                return DISPLAY_TYPE_TITLE;
            return DISPLAY_TYPE_SINGLE;
        } else if (obj instanceof List) {
            return DISPLAY_TYPE_DOUBLE;
        }
        return super.getItemViewType(position);
    }

    @Override
    protected Animator[] getAnimators(View view) {
        return new Animator[0];
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case DISPLAY_TYPE_TITLE:
                return new TitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_me_menu_title_layout, parent, false));
            case DISPLAY_TYPE_SINGLE:
                return new SingleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_me_option_menu_layout, parent, false));
            case DISPLAY_TYPE_DOUBLE:
                return new DoubleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_me_list_menu_layout, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.setPosition(position);
    }

    public abstract class ViewHolder<M> extends MyBaseRecyclerAdapter.MyViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }

        abstract void setPosition(int position);

        public M getModel(Object item) {
            return (M) item;
        }
    }

    public class TitleViewHolder extends ViewHolder<InfoMenu> {
        private RelativeLayout rlContainer;
        private TextView tvMenuTitle;

        public TitleViewHolder(View itemView) {
            super(itemView);
            rlContainer = (RelativeLayout) itemView.findViewById(R.id.rl_container);
            tvMenuTitle = (TextView) itemView.findViewById(R.id.tv_menu_title);
        }

        @Override
        void setPosition(int position) {
            InfoMenu infoMenu = getModel(getItem(getHolderPosition()));
            setData(infoMenu);
        }

        private void setData(InfoMenu infoMenu) {
            boolean hadData = infoMenu != null && StringUtils.isNotEmpty(infoMenu.moduleTitle);
            rlContainer.setVisibility(hadData ? View.VISIBLE : View.GONE);
            if (!hadData) return;
            tvMenuTitle.setText(infoMenu.moduleTitle);
        }
    }

    public class SingleViewHolder extends ViewHolder<InfoMenu> {
        RelativeLayout rlContainer;
        ImageView imgIcon;
        TextView tvTitle;
        ImageView imgNew;
        TextView tvDescr;

        public SingleViewHolder(View itemView) {
            super(itemView);
            rlContainer = (RelativeLayout) itemView.findViewById(R.id.rl_container);
            tvDescr = (TextView) itemView.findViewById(R.id.tv_desc);
            imgIcon = (ImageView) itemView.findViewById(R.id.iv_me_icon);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_me_title);
            imgNew = (ImageView) itemView.findViewById(R.id.iv_me_setting_new);
        }

        @Override
        void setPosition(int position) {
            InfoMenu infoMenu = getModel(getItem(getHolderPosition()));
            setData(infoMenu);
        }

        public void setData(final InfoMenu menu) {
            if (menu == null) return;
            imgIcon.setImageURI(Uri.parse(menu.icon_url));
            tvTitle.setText(menu.title);
            tvDescr.setText(StringUtils.isEmpty(menu.descr) ? "" : Html.fromHtml(menu.descr));

            // 判断该title下是否有红点提示
//            imgNew.setVisibility(NoticeManager.getInstance().hasNewFeature(menu.title) ? View.VISIBLE : View.INVISIBLE);
            imgNew.setVisibility(menu.is_new ? View.VISIBLE : View.INVISIBLE);

            rlContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickLinkView(v, RouterHelper.isNeedLoginAndHasLogin(menu.link_url), menu.title);
                }

                @EventTrace(EventID.APP_OWNER_LIST)
                private void clickLinkView(View v, @EventTraceWhenTrue boolean needAnalyse, @EventTraceParam(EventDataField.ParamId) String title) {
                    //隐藏红点
                    if (imgNew.isShown()) {
                        imgNew.setVisibility(View.INVISIBLE);
                        menu.is_new = false;
                        NoticeManager.getInstance().readNewFeature(menu.title);
                    }
                    //打开链接
                    Router.create(mContext, menu.link_url).open();
                }
            });
        }
    }

    public class DoubleViewHolder extends ViewHolder<List> {
        LinearLayout llContainer;
        RelativeLayout rlContainer1, rlContainer2;
        ImageView imgIcon1, imgIcon2;
        TextView tvTitle1, tvTitle2;
        TextView tvDescr1, tvDescr2;

        public DoubleViewHolder(View itemView) {
            super(itemView);
            llContainer = (LinearLayout) itemView.findViewById(R.id.ll_container);
            rlContainer1 = (RelativeLayout) itemView.findViewById(R.id.rl_menu_layout1);
            rlContainer2 = (RelativeLayout) itemView.findViewById(R.id.rl_menu_layout2);
            imgIcon1 = (ImageView) itemView.findViewById(R.id.iv_menu_logo1);
            imgIcon2 = (ImageView) itemView.findViewById(R.id.iv_menu_logo2);
            tvTitle1 = (TextView) itemView.findViewById(R.id.tv_menu_name1);
            tvTitle2 = (TextView) itemView.findViewById(R.id.tv_menu_name2);
            tvDescr1 = (TextView) itemView.findViewById(R.id.tv_menu_introduce1);
            tvDescr2 = (TextView) itemView.findViewById(R.id.tv_menu_introduce2);
        }

        @Override
        void setPosition(int position) {
            List infoMenuList = getModel(getItem(getHolderPosition()));
            setData(infoMenuList);
        }

        private void setData(List infoMenuList) {
            if (infoMenuList == null || infoMenuList.isEmpty()) {
                llContainer.setVisibility(View.GONE);
                return;
            }
            if (infoMenuList.size() == 1) {
                rlContainer2.setVisibility(View.INVISIBLE);
                InfoMenu menu = (InfoMenu) infoMenuList.get(0);
                if (menu == null) {
                    llContainer.setVisibility(View.GONE);
                    return;
                }
                imgIcon1.setImageURI(Uri.parse(menu.icon_url));
                tvTitle1.setText(menu.title);
                tvDescr1.setVisibility(StringUtils.isEmpty(menu.descr) ? View.GONE : View.VISIBLE);
                tvDescr1.setText(StringUtils.isEmpty(menu.descr) ? "" : Html.fromHtml(menu.descr));
            } else {
                final InfoMenu menu1 = (InfoMenu) infoMenuList.get(0);
                final InfoMenu menu2 = (InfoMenu) infoMenuList.get(1);
                if (menu1 == null && menu2 == null) {
                    llContainer.setVisibility(View.GONE);
                    return;
                } else if (menu1 == null) {
                    rlContainer1.setVisibility(View.INVISIBLE);
                    rlContainer2.setVisibility(View.VISIBLE);
                } else if (menu2 == null) {
                    rlContainer1.setVisibility(View.VISIBLE);
                    rlContainer2.setVisibility(View.INVISIBLE);
                } else {
                    rlContainer1.setVisibility(View.VISIBLE);
                    rlContainer2.setVisibility(View.VISIBLE);
                }
                if (menu1 != null) {
                    imgIcon1.setImageURI(Uri.parse(menu1.icon_url));
                    tvTitle1.setText(menu1.title);
                    tvDescr1.setVisibility(StringUtils.isEmpty(menu1.descr) ? View.GONE : View.VISIBLE);
                    tvDescr1.setText(StringUtils.isEmpty(menu1.descr) ? "" : Html.fromHtml(menu1.descr));
                    rlContainer1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            clickLinkView(v, RouterHelper.isNeedLoginAndHasLogin(menu1.link_url), menu1.title);
                        }

                        @EventTrace(EventID.APP_OWNER_LIST)
                        private void clickLinkView(View v, @EventTraceWhenTrue boolean needAnalyse, @EventTraceParam(EventDataField.ParamId) String title) {
                            //打开链接
                            Router.create(mContext, menu1.link_url).open();
                        }
                    });
                }
                if (menu2 != null) {
                    imgIcon2.setImageURI(Uri.parse(menu2.icon_url));
                    tvTitle2.setText(menu2.title);
                    tvDescr2.setVisibility(StringUtils.isEmpty(menu2.descr) ? View.GONE : View.VISIBLE);
                    tvDescr2.setText(StringUtils.isEmpty(menu2.descr) ? "" : Html.fromHtml(menu2.descr));
                    rlContainer2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            clickLinkView(v, RouterHelper.isNeedLoginAndHasLogin(menu2.link_url), menu2.title);
                        }

                        @EventTrace(EventID.APP_OWNER_LIST)
                        private void clickLinkView(View v, @EventTraceWhenTrue boolean needAnalyse, @EventTraceParam(EventDataField.ParamId) String title) {
                            //打开链接
                            Router.create(mContext, menu2.link_url).open();
                        }
                    });
                }
            }
            rlContainer2.setVisibility(View.VISIBLE);
        }
    }
}
